package mobi.workflowengine.core;

public abstract class WorkflowTask {
	
	WorkflowTaskEndCallback onEndCallback;	

	public WorkflowTask(WorkflowTaskEndCallback onEnd)
	{
		onEndCallback = onEnd;
	}
	
	public void start(WorkflowTask pParentWorkflowTask)
	{
		mCompletedSuccessfully = false;
		this.notifyWorkflowMessage(WorkflowStatus.Started.getValue());
	}
	
	public void cancel()
	{
		onCancel();
	}
	public void onNotification(int notificationCode)
	{
		
	}
	
	public void notifyWorkflowMessage(int workflowCode)
	{
		this.onNotification(workflowCode);
	}
	
	public boolean completedSuccessfully()
	{
		return mCompletedSuccessfully;
	}
	
	protected void onSuccessfulEnd()
	{
		mCompletedSuccessfully = true;
		
		if(onEndCallback!=null)
			onEndCallback.doThis(WorkflowStatus.SuccessfulEnd);
	}
	
	protected void onFailEnd()
	{
		if( onEndCallback!=null)
			onEndCallback.doThis(WorkflowStatus.Failed);
	}
	
	protected void onExceptionEnd()
	{
		if( onEndCallback!=null)
			onEndCallback.doThis(WorkflowStatus.FailedWithException);
	}
	
	protected void onCancel()
	{
		if( onEndCallback!=null)
			onEndCallback.doThis(WorkflowStatus.Canceled);
	}
	
	protected void onStuck()
	{
		if( onEndCallback!=null)
			onEndCallback.doThis(WorkflowStatus.Stuck);
	}
	
	protected boolean mCompletedSuccessfully;
	protected WorkflowStatus parentWorkflow;
	
}
